let userId = uuid();
let ws
let wsConnected;
$(function () {
    let wsUrl = `ws://${window.location.host}/websocket/${userId}`;
    if ('WebSocket' in window) {
        console.log("您的浏览器支持 WebSocket!");
    }

    function webSocketInit(url) {
        ws = new WebSocket(url);
        ws.onmessage = function (event) {
            let object = JSON.parse(event.data);
            switch (object.type) {
                case "test":
                    console.log('test ws received');
                    break;
                case "status":
                    statusGot(object.data);
                    let msgGot = document.getElementById('iframe').contentWindow.msgGot;
                    if (msgGot) msgGot(object);
                    break;
                case "alert":
                    alertGot(object.data);
                    break;
                case "meteorData":
                case "waveData":
                case "aisData":
                case "waterQuality":
                case "atmosData":
                case "adcpData":
                case "ctdData":
                case "radarData": {
                    let msgGot = document.getElementById('iframe').contentWindow.msgGot;
                    if (msgGot) msgGot(object);
                    break;
                }
                case "responseData":{
                    //要求把回执做成弹窗
                    console.log(object.data);
                    resGot(object.data);
                    break;
                }
                default:
                    break;
            }
        };

        function resGot(req){
            let cmdName = '';
            switch (req.cmdType){
                case "0":
                    cmdName = "模式控制";
                    break;
                case "1":
                    cmdName = "地址修改";
                    break;
                case "2":
                    cmdName = "发送模式"
                    break;
                case "3":
                    cmdName = "跑偏半径"
                    break;
                case "4":
                    cmdName = "轨迹点设置"
                    break;
                case "5":
                    cmdName = "航迹规划"
                    break;
                case "6":
                    cmdName = "数字自毁"
                    break;
                case "7":
                    cmdName = "舵机设置"
                    break;
                case "8":
                    cmdName = "舵角设置"
                    break;
                case "9":
                    cmdName = "负载控制"
                    break;
            }
            //$.modal.alertSuccess(req.boatCode + cmdName + "设置成功");
        }

        ws.onopen = function (event) {
            console.log('连接成功！');
            wsConnected = true;
            sendBoatCodes();
        };
        ws.onclose = function (event) {
            console.log('连接关闭！', new Date());
            wsConnected = false;
            reconnect(url);
        };
        ws.onerror = function (event) {
            console.log('连接出错！', new Date());
            wsConnected = false;
        };
    }

    function reconnect(url) {
        console.log("重连ws", new Date())
        setTimeout(function () {
            webSocketInit(url);
        }, 5000)
    }

    webSocketInit(wsUrl);
});

/**
 * 发送所有艇编号给后端
 */
function sendBoatCodes() {
    if (boats.length !== 0 && wsConnected) ws.send(`codes:${boats.map(boat => boat.code)}`);
}